<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_UserRightsModel extends ADS_DefaultModel {
     
    public function getUserRights($userId) {
        if(!is_int($userId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetchAll('
          SELECT            
            *
          FROM
            [:wp:ads_user_rights] AS t1
          WHERE
            userId = %i
        ', $userId);
    }
    
    public function canDo($userId, $assetId, $right) {
        if(!is_int($userId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetchAll('
          SELECT            
            *
          FROM
            [:wp:ads_user_rights] AS t1
          WHERE
            userId = %i
          AND
            assetId = %i
          AND
            user_right = %s
        ', $userId, $assetId, $right);
    }
    
    public function addUserRight($userId, $assetId, $right){
        if(!is_int($userId) || !is_int($assetId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        //
        dibi::query('INSERT INTO [:wp:ads_user_rights] SET userId = %i, assetId = %i, user_right = %s', $userId, $assetId, $right);
    }
    
    public function removeAllRights($userId){
        if(!is_int($userId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %.',__METHOD__));
        }
        //update positions
        dibi::query('DELETE FROM [:wp:ads_user_rights] WHERE userId = %i', $userId);
    }

}